import { NModalProvider, type ModalProviderProps } from "naive-ui";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";
import type { HTMLAttributes } from "vue";

export type ModalProviderFactoryConstructorType = {
  props?: ModalProviderProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
};

export class ModalProviderFactory {
  private defaultSlot: BaseComponentType = null;
  private attrs: HTMLAttributes = {};
  private props: ModalProviderProps = {};

  constructor(param?: ModalProviderFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.attrs) this.setAttrs(param.attrs);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
  }

  setProps(props: ModalProviderProps) {
    this.props = props;
    return this;
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }

  create() {
    return (
      <NModalProvider {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
        }}
      </NModalProvider>
    );
  }
}
